/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package bd.commons.math.distribution;

import bd.org.apache.commons.math.distribution.AbstractIntegerDistribution;

import commons.util.ExceptionUtil;

import java.math.BigDecimal;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Implements an integer distribution with discrete probabilities. Currently, there is no
 * implementation in Commons Apache on this.
 *
 * @author Susanta Tewari
 * @version 1.0.1
 * @history Created on May 23, 2012.
 * @since 1.0.1
 */
public class ConnectedIntegerDistribution extends AbstractIntegerDistribution {

    /** Field description */
    private static final long serialVersionUID = 1L;

    /** Field description */
    private BigDecimal mean, variance;

    /** Field description */
    private SortedMap<Integer, BigDecimal> dist;

    /**
     * @param dist keys are consecutive integers; values their probabilities
     */
    public ConnectedIntegerDistribution(SortedMap<Integer, BigDecimal> dist) {

        if (dist == null) {
            ExceptionUtil.throwIllegalArgExNull("dist");
        }

        if (dist.isEmpty()) {
            throw new IllegalArgumentException("empty dist");
        }

        // todo check the keys are connected

        this.dist = new TreeMap<>(dist);

        // compute mean
        mean = BigDecimal.ZERO;

        for (Integer key : dist.keySet()) {

            BigDecimal prob = dist.get(key);

            mean = mean.add(prob.multiply(new BigDecimal(key)));
        }

        // compute variance
        BigDecimal eX2 = BigDecimal.ZERO;

        for (Integer key : dist.keySet()) {

            BigDecimal prob = dist.get(key);

            eX2 = eX2.add(prob.multiply(new BigDecimal(key).pow(2)));
        }

        variance = eX2.subtract(mean.pow(2));
    }

    /**
     * Method description
     *
     *
     * @param x
     *
     * @return
     */
    @Override
    public double probability(int x) {

        if ((x < getSupportLowerBound()) || (x > getSupportUpperBound())) {

            return 0;
        }

        return dist.get(x).doubleValue();
    }


    /**
     * Method description
     *
     *
     * @param x
     *
     * @return
     */
    public BigDecimal probabilityBD(int x) {

        if ((x < getSupportLowerBound()) || (x > getSupportUpperBound())) {

            return BigDecimal.ZERO;
        }

        return dist.get(x);
    }


    /**
     * Method description
     *
     *
     * @param x
     *
     * @return
     */
    @Override
    public double cumulativeProbability(int x) {

        if (x < getSupportLowerBound()) {
            return 0;
        }

        if (x > getSupportUpperBound()) {
            return 1;
        }


        BigDecimal result = dist.get(x);

        for (Integer key : dist.headMap(x).keySet()) {

            BigDecimal prob = dist.get(key);

            result = result.add(prob);
        }

        return result.doubleValue();
    }


    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public double getNumericalMean() {

        return mean.doubleValue();
    }


    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public double getNumericalVariance() {

        return variance.doubleValue();
    }


    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public int getSupportLowerBound() {

        return dist.firstKey();
    }


    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public int getSupportUpperBound() {

        return dist.lastKey();
    }


    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public boolean isSupportConnected() {

        return true;
    }
}
